﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace GZ.Common
{
    /// <summary>
    /// 数据导出类
    /// </summary>
    public class DataExportHelper
    {
        /// <summary>
        /// 将DataSet导出成CSV格式
        /// </summary>
        /// <param name="ds">DataSet</param>
        /// <returns>CSV字符串数据</returns>
        public static string ExportCSV(DataTable dt)
        {
            string data = "";
            //写出列名
            foreach (DataColumn column in dt.Columns)
            {
                data += column.ColumnName + ",";
            }
            data += "\n";
            //写出数据
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn column in dt.Columns)
                {
                    data += row[column].ToString() + ",";
                }
                data += "\n";
            }
            data += "\n";

            return data;
        }
    }
}
